<template>

<div>
  <el-row>
    <mu-appbar v-if="!this.isGm1" title="代理总人数" style="background-color:#409EFF">
      <mu-icon-button  icon="arrow_back_ios" slot="left" :to="{name: 'AgentMenu' }"/>
    </mu-appbar>
    <mu-appbar v-if="this.isGm1" title="代理详情" style="background-color:#409EFF">
      <mu-icon-button  icon="arrow_back_ios" slot="left" :to="{name: 'Home' }"/>
    </mu-appbar>
  </el-row>
  <el-row>
    <el-col :span="12">
    </el-col>
    <el-col :span="10">
      <center>
        <el-tag size="mini">新{{newAgent}}</el-tag>
        <el-tag type="success" size="mini">总{{totalAgent}}</el-tag>
      </center>
    </el-col>
    <el-col :span="2">
      <i class="el-icon-arrow-down" style="float:right" @click="handleTools"></i>
    </el-col>
  </el-row>

  <div class= "tools" v-show="tools">
    <el-row>
      <el-col :xs="6" :sm="4" :md="3" :lg="3">
        代理ID
      </el-col>
      <el-col :xs="18" :sm="20" :md="21" :lg="21">
        <el-input v-model="condition.uid" size="mini" @blur="getAgents"></el-input>
      </el-col>
    </el-row>
  </div>

  <el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      size="mini">
      <el-table-column
        prop="agentUid"
        label="代理ID">
      </el-table-column>
      <el-table-column
        prop="binderUid"
        label="上级ID">
      </el-table-column>
      <!-- <el-table-column
        v-if="!this.isAgent"
        prop="organizer"
        label="顶级ID">
      </el-table-column> -->
      <!-- <el-table-column
        v-if="!this.isAgent"
        prop="agentLevel"
        label="代理级别">
        <template slot-scope="scope">
          <span v-if="scope.row.agentLevel === 1">二级代理</span>
          <span v-if="scope.row.agentLevel === 2">一级代理</span>
          <span v-if="scope.row.agentLevel === 3">总代理</span>
          <span v-if="scope.row.agentLevel === 4">运营商</span>
          <span v-if="scope.row.agentLevel === 6">白户</span>
          <span v-if="scope.row.agentLevel === 5">官方</span>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="wechat"
        label="Line">
      </el-table-column>
      <el-table-column
        prop="realName"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="授权时间">
      </el-table-column>
    </el-table>
  </el-row>

  <el-row>
    <center>
      <el-pagination
        small
        layout="prev, pager, next"
        :total="condition.total"
        :page-size="condition.pageSize"
        :current-page="condition.currentPage"
        @current-change="handleCurrentChange">
      </el-pagination>
    </center>
  </el-row>

</div>

</template>

<style scoped>
  .el-row {
    margin-bottom: 16px;
  }
  .el-col {
    margin-bottom: 8px;
  }
  .el-dialog {
    width: 70%
  }
</style>

<script>
  import moment from 'moment'
  export default {
    data () {
      return {
        tools: true,
        newAgent: 0,
        totalAgent: 0,
        tableData: [

        ],
        condition: {
          startDay: this.$time.getDateBefore(new Date(), 0),
          endDay: this.$time.getDateBefore(new Date(), 0),
          account: sessionStorage.getItem('account'),
          uid: '',
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        isAgent: false,
        isGm1: false
      }
    },
    methods: {
      handleTools () {
        if (this.tools) {
          this.tools = false
        } else {
          this.tools = true
        }
      },

      getNewAgentNumber () {
        let that = this
        this.$axios({
          method: 'get',
          url: '/api/AgentService.getNewAgentNumber',
          params: this.condition
        })
        .then(function (resp) {
          that.newAgent = resp.data.data
        })
      },
      getAgentNum () {
        let that = this
        this.$axios({
          method: 'get',
          url: '/api/AgentService.getAgentNum',
          params: {'account': sessionStorage.getItem('account')}
        })
        .then(function (resp) {
          that.totalAgent = resp.data.data
        })
      },
      getAgents () {
        let that = this
        if (this.condition.uid === '' || this.condition.uid.length === 0) {
          this.condition.uid = -1
        }
        this.$axios({
          method: 'get',
          url: '/api/AgentService.getAgents',
          params: this.condition
        })
        .then(function (resp) {
          that.initCondition()
          for (let item in resp.data.data) {
            resp.data.data[item].createTime = moment.utc(resp.data.data[item].createTime).zone(-8).format('YYYY-MM-DD HH:mm:ss')
          }
          that.tableData = resp.data.data
          that.condition.total = resp.data.page.total
        })
      },
      init () {
        this.getAgents()
        this.getAgentNum()
        this.getNewAgentNumber()
      },
      initCondition () {
        this.condition.uid = ''
        this.condition.binderUid = ''
      },
      handleCurrentChange (val) {
        this.condition.currentPage = val
        this.getAgents()
      }
    },
    mounted () {
      if (sessionStorage.getItem('role') === 'agent') {
        this.isAgent = true
      }
      if (sessionStorage.getItem('role') === 'gm1') {
        this.isGm1 = true
      }
      this.init()
    }
  }
</script>
